package com.hns.can.master.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.constants.GlobalConst;
import com.hns.can.common.entity.BaseEntity;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.common.formula.FillParamsVo;
import com.hns.can.master.entity.BaseEqmtInfoEntity;
import com.hns.can.master.vo.EqtPostDataVo;
import com.hns.frame.enumrate.EnabledSt;
import com.hns.frame.enumrate.IsClosed;
import com.hns.frame.mybatis.Pagination;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.PubUtil;
import com.hns.tool.pub.UtilString;

/**
 * 设备信息维护业务类
 * @author:lan
 * @email:763836737@qq.com
 * @version Revision 2.0.0
 */
@Service
public class EquipmentInfoService extends CommonService {
    
	/**
	 * 填充代码描述
	 */
    @Autowired
    private FillCodeDscValue fillCodeDscValue;
    /**
     * 添加一个设备
     * @param entity
     * @param user
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    @CacheEvict(value=GlobalConst.CACHE_KEY_BASE_EQMT_INFO, key="#user.getCorpId()")
    public void addEquipmentInfo(BaseEqmtInfoEntity entity, SysUserEntity user) {
        // 补充实体的其他信息
        entity.setEqmtId(UtilString.getUUID());
        entity.setEnabledSt(EnabledSt.ENABLE.getFlag());  // 启用状态
        entity.setCrtTime(new Date());
        entity.setCrtUser(user.getAcctName());
        entity.setUpdateTime(new Date());
        entity.setUpdateUser(user.getAcctName());
        entity.setIsClosed(IsClosed.ENABLE.getFlag());
        
        // 后台校验
        // -- 设备ID eqmtCd
        this.check("equipmentInfo.checkEqmtCd", entity, "您输入的设备ID已经存在！");
        // -- SIM卡号 simCardno
        if(PubUtil.isNotEmpty(entity.getSimCardno())){
            this.check("equipmentInfo.checkSimCardno", entity, "您输入的SIM卡号已经存在！");
        }
        commonEntityDao.save(entity);
    }
    
    /**
     * 得到设备page对象
     * @param dataVo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    public Pagination searchEquipmentInfoPage(EqtPostDataVo dataVo) throws Exception {
        Pagination page = this.myBatisPage("equipmentInfo.getEquipmentInfoCnt","equipmentInfo.getEquipmentInfoList",dataVo,BaseEqmtInfoEntity.class);
        Map<String,String> map = new HashMap<String,String>();
        map.put("organName", GlobalConst.DEFAULT_CACHE_VALUE);   
        FillParamsVo vo = new FillParamsVo("organId", map);
//        fillCodeDscValue.fillInfoValue(page.getData(), vo, GlobalConst.CACHE_KEY_BASE_ORGAN);
        List<Object> list = page.getData();
        fillCodeDscValue.fillInfoValue(list, new String[]{GlobalConst.CACHE_KEY_BASE_ORGAN}, vo);
        page.setData(list);
        return page;
    }
    
    /**
     * 得到设备信息list对象
     * @param dataVo
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @SuppressWarnings({"unchecked" })
    @Transactional
    public List<BaseEntity> getEquipmentInfoList(EqtPostDataVo dataVo) throws Exception {
        List<BaseEntity> list = PubUtil.convertListMapToObject(this.sqlMapDao.selectList("equipmentInfo.getEquipmentInfoList", dataVo), BaseEqmtInfoEntity.class);
        Map<String,String> mapTmp = new HashMap<String,String>();
        mapTmp.put("eqmtSt", "EQS00000");
        mapTmp.put("produceFacty", "EQFA0000");
        mapTmp.put("organId", "ORG00000");
        fillCodeDscValue.fillCodeDscValue(list, mapTmp);
        return list;
    }
    
    /**
     * 根据设备id求设备信息
     * @param eqmtId
     * @return
     * @throws Exception
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    public BaseEqmtInfoEntity getEquipmentInfoByEqmtId(String eqmtId) throws Exception {
        BaseEqmtInfoEntity entity = commonEntityDao.find(eqmtId, BaseEqmtInfoEntity.class);
        List<BaseEntity> list = new ArrayList<BaseEntity>();
        list.add(entity);
        Map<String,String> map = new HashMap<String,String>();
        map.put("corpName", GlobalConst.DEFAULT_CACHE_VALUE);   
        FillParamsVo vo = new FillParamsVo("corpId", map);
        map = new HashMap<String,String>();
        map.put("organName", GlobalConst.DEFAULT_CACHE_VALUE);   
        FillParamsVo vo2 = new FillParamsVo("organId", map);
        map = new HashMap<String,String>();
        map.put("licPltNo", GlobalConst.DEFAULT_CACHE_VALUE);  
        FillParamsVo vo3 = new FillParamsVo("rltvCarId", map);
        fillCodeDscValue.fillInfoValue(list, new String[]{GlobalConst.CACHE_KEY_SYS_CORP, GlobalConst.CACHE_KEY_BASE_ORGAN, GlobalConst.CACHE_KEY_BASE_CAR_INFO}, vo, vo2, vo3);
        
        return entity;
    }
    
    /**
     * 更新一个设备的信息
     * @param user
     * @param entity
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    @CacheEvict(value=GlobalConst.CACHE_KEY_BASE_EQMT_INFO, key="#user.getCorpId()")
    public void uptEquipmentInfo(SysUserEntity user, BaseEqmtInfoEntity entity){
        BaseEqmtInfoEntity tmpEntity = commonEntityDao.find(entity.getEqmtId(), BaseEqmtInfoEntity.class);
        entity.setEnabledSt(tmpEntity.getEnabledSt());  // 启用状态
        entity.setCrtTime(tmpEntity.getCrtTime());
        entity.setCrtUser(tmpEntity.getCrtUser());
        entity.setUpdateTime(new Date());
        entity.setUpdateUser(user.getAcctName());
        entity.setIsClosed(IsClosed.ENABLE.getFlag());
        
        // 后台校验
        // -- 设备ID eqmtCd
        this.check("equipmentInfo.checkEqmtCd", entity, "您输入的设备ID已经存在！");
        // -- SIM卡号 simCardno
        if(PubUtil.isNotEmpty(entity.getSimCardno())){
            this.check("equipmentInfo.checkSimCardno", entity, "您输入的SIM卡号已经存在！");
        }
        commonEntityDao.update(entity);
    }
    
    /**
     *  删除一个设备
     * @param user
     * @param eqmtIds
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    @CacheEvict(value=GlobalConst.CACHE_KEY_BASE_EQMT_INFO, key="#user.getCorpId()")
    public void delEquipmentInfo(SysUserEntity user, String[] eqmtIds){
        for (String eqmtId : eqmtIds) {
            BaseEqmtInfoEntity entity = commonEntityDao.find(eqmtId, BaseEqmtInfoEntity.class);
            entity.setIsClosed(IsClosed.DISABLE.getFlag());
            entity.setEnabledSt(EnabledSt.DISABLE.getFlag());
            entity.setUpdateTime(new Date());
            entity.setUpdateUser(user.getAcctName());
            commonEntityDao.update(entity);
        }
    }
    
    /**
     * 校验关联车辆
     * @param rltvCarId
     * @return
     * @author:lan
     * @email:763836737@qq.com
     */
    @Transactional
    public boolean validateRltvCarId(String rltvCarId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("rltvCarId", rltvCarId);
        params.put("enabledSt", EnabledSt.ENABLE.getFlag());
        List<BaseEqmtInfoEntity> list = commonEntityDao.findByParamsIgnoreNullValue(BaseEqmtInfoEntity.class, params);
        if(list.size() > 0){
            return true;
        }
        return false;
    }
    
    /**
     * 根据eqmtCd获取设备信息
     * @param eqmtCd
     * @return
     * @author:lan
     * @email:763836737@qq.com
     */
    public BaseEqmtInfoEntity getEquipmentInfoByEqmtCd(String eqmtCd){
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("eqmtCd", eqmtCd);
        params.put("enabledSt", EnabledSt.ENABLE.getFlag());
        List<BaseEqmtInfoEntity> list = commonEntityDao.findByParamsIgnoreNullValue(BaseEqmtInfoEntity.class, params);
        if(PubUtil.isNotEmpty(list)){
            return list.get(0);
        }else{
            return null;
        }
    }
    
}
